\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Numerical Analysis Homework}
\author{3200102450 ZhangWen}
\date{October 2022}

\begin{document}

\maketitle

\section*{A}
Solution:

We should build a class function,including its value and its derivative.
Because it isn't a specific function,we should make virtual class.
After this, we build a class for EquationSolver including virtual function solve.

Then,we should build classes for Bisection,Newton,Secant method based on EquationSolver.

\begin{verbatim}
void swap(double &a, double &b)
{
	double t; 
	t = a;
	a = b;
	b = t;
}

class Fun
{
public:
	virtual double f(double x)
	{
		return 0;
	}
	virtual double df(double x)
	{
		return 0;
	}
};

class EquationSolver
{
public:
	virtual double solve() { return 0; }
};

class Bisection : public EquationSolver
{
private:
	Fun & f;
	double a, b, delta, eps;
	int M;
public:
	Bisection(Fun & _f, double _a, double _b, double _delta, double _eps, int _M):
	f(_f), a(_a), b(_b), delta(_delta), eps(_eps), M(_M) {}
	double solve()
	{
		double h = b - a, c = b, l = a, r = b;
		for (int i = 1; i <= M; i++)
		{
			h = h / 2, c = a + h, l = f.f(a), r = f.f(c);
			if (fabs(h) < delta || fabs(r) < eps)  break;
			else if (l * r > 0)  a = c;
		}
		return c;
	}
};

class Newton : public EquationSolver
{
private:
	Fun & f;
	double a, eps;
	int M;
public:
	Newton(Fun & _f, double _a, double _eps, int _M):
	f(_f), a(_a), eps(_eps), M(_M) {}
	double solve()
	{
		double v, d, b;
		for (int i = 1; i <= M; i++)
		{
			v = f.f(a), d = f.df(a);
			b = a - v / d;
			if (fabs(f.f(b)) < eps)  break;
			a = b;
		}
		return b;
	}
};

class Secant : public EquationSolver
{
private:
	Fun & f;
	double a, b, delta, eps;
	int M;
public:
	Secant(Fun & _f, double _a, double _b, double _delta, double _eps, int _M):
	f(_f), a(_a), b(_b), delta(_delta), eps(_eps), M(_M) {}
	double solve()
	{
		double u = f.f(a), v = f.f(b);
		for (int i = 2; i <= M; i++)
		{
			if (fabs(u) < fabs(v))  swap(a, b), swap(u, v);
			a = b - v * (b - a) / (v - u);
			if (fabs(a - b) < delta || fabs(a) < eps) break;
			u = f.f(a), v = f.f(b);
		}
		return a;
	}
};
\end{verbatim}
\section*{B}
Solution:

The solution of $\frac{1}{x}-\tan{x}$ is 0.860276.

The solution of $\frac{1}{x}-2^x$ is 0.641174.

The solution of $2^{-x}+e^x+2\cos(x)-6$ is 1.829407.

The solution of $\frac{x^3+4x^2+3x+5}{2x^3-9x^2+18x-2}$ is 0.117859.

\section*{C}
Solution:

The solution of $x-\tan(x)$ near 4.5 is 4.493410.

The solution of $x-\tan(x)$ near 7.7 is 7.725252.

\section*{D}
Solution:

The solution of $\sin(\frac{x}{2})-1$ is 3.141437.

The solution of $e^x-\tan(x)$ is 1.306327.

The solution of $x^3-12x^2+3x+1$ is -0.188685.

\section*{E}
Solution:

The solution by Bisection method is 0.835938.

The solution by Newton method is 0.833834.

The solution of Secant method is 0.833836.

\section*{F}
Solution:

(a)The solution is 32.972184.

(b)The solution is 33.168904.

(c1)The solution is 33.168914 with the initial value 33 and 60 degrees.

(c2)The solution is 168.500000 with the initial value 160 and 180 degrees.

We can find that if the initial values are 160 and 180 degrees,the solution will be
168.5 degrees.

The reason for different result is that there are countable real results.If we choose the initial values far away 33 degrees,the result is far away 33 degrees.
\end{document}
